package controllers

import (
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis/v8"
	"pets.ser163.cn/config"
)

func GetRedisData(c *gin.Context) {

	key := c.Query("key")
	if key == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Key is required"})
		return
	}

	value, err := config.R.GetValue(key)
	if err == redis.Nil {
		c.JSON(http.StatusNotFound, gin.H{"message": "Key not found"})
	} else if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
	} else {
		c.JSON(http.StatusOK, gin.H{"key": value})
	}
}

func SetRedisData(c *gin.Context) {

	key := c.Query("key")
	value := c.Query("value")
	if key == "" || value == "" {
		c.JSON(http.StatusBadRequest, gin.H{"error": "Key and value are required"})
		return
	}

	if err := config.R.SetValue(key, value); err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Key set successfully"})
}
